package com.enqualcomm.kids.activities;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.enqualcomm.kids.R;
import com.enqualcomm.kids.base.BaseActivity;
import com.enqualcomm.kids.bean.StringMessage;
import com.enqualcomm.kids.config.userdefault.AppDefault;
import com.enqualcomm.kids.config.userdefault.UserDefault;
import com.enqualcomm.kids.config.userdefault.UserTerminalDefault;
import com.enqualcomm.kids.mvp.bindphoneaccount.BindPhoneAccountView;
import com.enqualcomm.kids.mvp.bindphoneaccount.Presenter;
import com.enqualcomm.kids.mvp.login.wx.view.BindPhoneView;
import com.enqualcomm.kids.network.socket.response.BindUserRelationSetResult;
import com.enqualcomm.kids.network.socket.response.LoginOtherResult;
import com.enqualcomm.kids.network.socket.response.TerminallistResult;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.rest.Accept;

import common.utils.PermissionUtils;
import common.utils.PromptUtil;
import de.greenrobot.event.EventBus;

/**
 * Created by Sunzhigang on 2016/9/14.
 */
@EActivity(R.layout.activity_bind_phone_account)
public class BindPhoneAccountActivity extends BaseActivity implements BindPhoneAccountView {
    @ViewById(R.id.phone_number_et)
    EditText phone_number_et;
    @ViewById(R.id.password_et)
    EditText password_et;
    @ViewById(R.id.country_regin_tv)
    TextView country_regin_tv;
    Presenter presenter;
    @ViewById(R.id.title_bar_title_tv)
    TextView title_bar_title_tv;
    private TerminallistResult.Terminal terminal;

    @AfterViews
    void setupViews() {
        presenter = new Presenter(this);
        terminal = getTerminal();
        TextView title_bar_terminal_name_tv = (TextView) findViewById(R.id.title_bar_terminal_name_tv);
        View title_bar_terminal_icon_iv = findViewById(R.id.title_bar_terminal_icon_iv);
        title_bar_terminal_icon_iv.setVisibility(View.GONE);
        title_bar_terminal_name_tv.setVisibility(View.VISIBLE);
        title_bar_terminal_name_tv.setText(getString(R.string.system_settings));
//        title_bar_title_tv.setText(R.string.bind_phone_account_dong);
        title_bar_terminal_name_tv.setText(R.string.bind_phone_account_dong);

    }

    @Override
    public String getCountryCode() {
        String conutryCode = country_regin_tv.getText().toString().trim();
        if (conutryCode.equals(getString(R.string.watch_numer_country_regin))) {
            conutryCode = "";
        } else if (!TextUtils.isEmpty(conutryCode)) {
            conutryCode = conutryCode.substring(1, conutryCode.length());
        }
        return conutryCode;
    }

    @Override
    public void bindSuccess(LoginOtherResult result) {
        hideProgress();
        EventBus.getDefault().post(new StringMessage(StringMessage.REFRESH_TERMINALLIST));
        finish();
    }

    @Override
    public void bindFailed(int resID) {
        hideProgress();
        PromptUtil.toast(this, resID);
    }

    @Override
    public void bindFailed(String message) {
        hideProgress();
        PromptUtil.toast(this, message);
    }


    @Override
    public String getAccount() {
        return phone_number_et.getText().toString().trim();
    }

    @Override
    public String getPassword() {
        return password_et.getText().toString().trim();
    }

    @Click(R.id.done_btn)
    void bind() {
        showProgress();
        presenter.bindPhoneAccount();
    }

    @Click(R.id.title_bar_left_iv)
    void goback() {
        finish();
    }

    @Click(R.id.country_regin_tv)
    void chooseCountryCode() {
        startActivityForResult(new Intent(this, ChooseCountryActivity.class), 10000);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10000 && resultCode == 10001) {
            String countryCode = data.getStringExtra("number");
            String name = data.getStringExtra("name");

            country_regin_tv.setText(name);
            if (countryCode.length() > 1) {
                country_regin_tv.setText(countryCode);
            } else {
                country_regin_tv.setText("");
            }
        }
    }

}
